define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/review/index',
                    add_url: 'order/review/add',
                    edit_url: 'order/review/edit',
                    del_url: 'order/review/del',
                    multi_url: 'order/review/multi',
                    table: 'review',
                    ajax_url:{
                        btn_1:"order/review/edit",//详情
                        btn_4:{data:{'method':"1"},url:"order/order/update_status",fc:function(data){
                                return true;
                            }},//驳回
                    },
                    btn_mall_window:{
                        btn_array:["审核通过","驳回"],
                        btn_evn_array_length:2,
                        btn_evn_array: {
                            evn_1:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                form.append(" <input name=\"type\"  value=\"1\" style=\"display: none\">");
                                var success = function (data,ret) {
                                    Layer.msg(data.responseJSON.msg);
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                                Fast.api.form_ajax_submit(form,success);
                            },
                            evn_2:function (index,layero) {
                                var frame = Layer.getChildFrame('html', index);
                                var form = $("form",frame);
                                form.append(" <input name=\"type\"  value=\"2\" style=\"display: none\">");
                                var success = function (data,ret) {
                                    Layer.msg(data.responseJSON.msg);
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                                Fast.api.form_ajax_submit(form,success);
                            }
                        },
                        area:['800px','400px']
                    }
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'review_id',
                sortName: 'review_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'order_sno', title: __('Order_sno')},
                        {field: 'n.username', title: __('Ad_username')},
                        {field: 'shop_name', title: __('Shopname')},
                        {field: 'r.username', title: __('Paike_name')},
                        {field: 'sd_shop_order.tao_bao_no', title: __('Tao_bao_no')},
                        {field: 'sd_shop_order.create_time', title: __('On_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'sd_shop_order.status', title: __('Status'), searchList: {
                                "0":__('已拍下'),
                                "1":__('未审核'),
                                "2":__('已审核'),
                                "3":__('已上传'),
                                "4":__('已复查'),
                                "5":__('已付款'),
                                "6":__('已下单'),
                                "7":__('待发货'),
                                "8":__('待好评'),
                                "9":__('已发货'),
                                "10":__('已好评'),
                                "11":__('待追评'),
                                "12":__('已追评'),
                                "13":__('已完成'),
                            },formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                if(value==undefined || value=="null"){
                                    value =   value
                                }else{
                                    value = value.toString();
                                }
                                var index = keys.indexOf(value);
                                value = value === null ? '' : value;
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {
                            field: 'p.auth_status',
                            title: __('Auth_status'),
                            searchList: {"0":__('未审核'),"1":__('已审核'),"2":__("已驳回")},
                            formatter:function(value,row,index){
                                var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                                if(value==undefined || value=="null"){
                                    value =   value
                                }else{
                                    value = value.toString();
                                }
                                var index = keys.indexOf(value);
                                value = value === null ? '' : value;
                                var display = index > -1 ? this.searchList[value] : null;
                                var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                                if (!display) {
                                    display = __(value.charAt(0).toUpperCase() + value.slice(1));
                                }
                                var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';

                                return html;
                            }},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter:function(value,row,index) {

                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);
                                var text_name;
                                console.log(row.order);
                                // if(row.order_state==0 || row.order_state==2) {
                                //      text_name = "撤销好评";
                                // }
                                // if(row.order_state==1){
                                //      text_name = "已撤销好评";
                                // }


                                if(row.auth_status==0 || row.auth_status==2) {
                                    buttons.push(
                                        {
                                            name: 'btn_1',
                                            text: "追评详情",
                                            extend: 'data-toggle="tooltip" data-name="btn_1" data-title="好评详情"',
                                            classname: 'btn btn-xs btn-success btn-magic btn-small-window'
                                        });
                                }else{
                                    buttons.push(
                                        {
                                            name: 'btn_1',
                                            text: "追评详情",
                                            extend: 'data-toggle="tooltip" data-name="btn_1" data-title="好评详情"',
                                            classname: 'btn btn-xs btn-success btn-magic btn-editone'
                                        });
                                }

                                // if(row.order.status!=14) {
                                //     //已撤销
                                //     if (row.order.order_state == 0 || row.order.order_state == 2) {
                                //         buttons.push(
                                //             {
                                //                 name: 'btn_2',
                                //                 text: text_name,
                                //                 extend: 'data-toggle="tooltip" data-name="btn_2" data-title="撤销好评"',
                                //                 classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                //             });
                                //     }
                                // }


                                //回收
                                // if(row.order.order_state==1){
                                //
                                //     buttons.push(
                                //         {
                                //             name: 'btn_2',
                                //             text: "回收好评",
                                //             extend: 'data-toggle="tooltip" data-name="btn_2" data-title="撤销好评"',
                                //             classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                //         });
                                //
                                // }
                                    //审核
                                    if (row.auth_status == 0) {
                                        buttons.push(
                                            {
                                                name: 'btn_3',
                                                text: "审核",
                                                extend: 'data-toggle="tooltip" data-name="btn_4" data-title="撤销好评"',
                                                classname: 'btn btn-xs btn-success btn-magic btn-ajax '
                                            }
                                        );
                                    }
                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(function(){
                //console.log($(".example2"))
                require(['img_css/easy-loading','img_css/hammer.min','img_css/jquery.mousewheel.min','img_css/zoom-marker'],function(){
                    // $('#zoom-marker-img').zoomMarker({
                    //     src: $('#zoom-marker-img').data("url"),
                    //     rate: 0.2,
                    //     width: 100,
                    //     max: 1000,
                    //     markers:[
                    //         //{src:"img/marker.svg", x:500, y:500}
                    //     ]
                    // });
                    $("img").each(function(){
                        console.log($(this).data("url"));
                        if($(this).data("url")!=undefined && $(this).data("url")!="") {
                            $(this).zoomMarker({
                                src: $(this).data("url"),
                                rate: 0.1,
                                min: 100,
                                width: 100,
                                max: 3000,
                                markers: [
                                    //{src:"img/marker.svg", x:500, y:500}
                                ],
                            });
                        }
                    });
                    // $('#zoom-marker-img-alt').zoomMarker({
                    //     src: $('#zoom-marker-img-alt').data("url"),
                    //     rate: 0.1,
                    //     min:100,
                    //     width: 100,
                    //     max: 3000,
                    //     markers:[
                    //         //{src:"img/marker.svg", x:500, y:500}
                    //     ]
                    //
                    //
                    // });
                })
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});